/*
 * Copyright (C) 2001-2005 Pleasant nightmare studio
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.pleasantnightmare.noraxidium;

import org.apache.log4j.Logger;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.SlickException;
import org.pleasantnightmare.noraxidium.support.DisplaySupport;
import org.pleasantnightmare.noraxidium.support.MapSupport;
import org.pleasantnightmare.noraxidium.support.ResourcesSupport;
import org.pleasantnightmare.noraxidium.support.Support;
import org.pleasantnightmare.noraxidium.util.ErrorHandling;
import org.pleasantnightmare.noraxidium.util.ParamArg;

import java.util.HashMap;
import java.util.Map;

/**
 * @author deus
 * @version 1.0
 * @since Jul 6, 2010 2:22:56 PM
 */
public class NoraxidiumGameContainer extends AppGameContainer implements SupportContainer {
  private static final Logger LOGGER = Logger.getLogger(NoraxidiumGameContainer.class);

  @ParamArg private String startingMap;
  @ParamArg private int resolutionWidth;
  @ParamArg private int resolutionHeight;
  @ParamArg private boolean fullscreen;
  @ParamArg private boolean debug;

  private Map<Class<? extends Support>, Support> supports;

  public NoraxidiumGameContainer(NoraxidiumGame game) throws SlickException {
    super(game);
    startingMap = "maps/prototype.tmx";
    resolutionWidth = 1024;
    resolutionHeight = 768;
    fullscreen = false;
    supports = new HashMap<Class<? extends Support>, Support>();
    debug = false;
  }

  public String getStartingMap() {
    return startingMap;
  }

  public void initialize() {
    try {
      setDisplayMode(resolutionWidth, resolutionHeight, fullscreen);
      initSupports();
    } catch (SlickException e) {
      throw ErrorHandling.wrap(e);
    }
  }

  private void initSupports() {
    MapSupport mapSupport = new MapSupport();
    mapSupport.setSupportContainer(this);
    supports.put(MapSupport.class, mapSupport);

    ResourcesSupport resourcesSupport = new ResourcesSupport();
    resourcesSupport.setSupportContainer(this);
    supports.put(ResourcesSupport.class, resourcesSupport);

    DisplaySupport screenSupport = new DisplaySupport();
    screenSupport.setSupportContainer(this);
    supports.put(DisplaySupport.class, screenSupport);
  }

  public <T extends Support> T getSupport(Class<T> supportType) {
    return (T) supports.get(supportType);
  }

  public boolean isDebug() {
    return debug;
  }
}
